package cn.gqr.controller;

import cn.gqr.entity.*;
import cn.gqr.service.BlogService;
import cn.gqr.service.TypeService;
import cn.gqr.service.UserService;
import cn.gqr.uitl.RespJson;
import cn.gqr.uitl.SessionUitl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("api/hello")
public class ClientController {

    @Autowired
    BlogService bs;

    @Autowired
    UserService us;

    @Autowired
    TypeService ts;

    @RequestMapping("/home")
    public String home(Model model){
        return "client/userInfo";
    }

    @RequestMapping("/collect")
    public String goCollect(){
        return "client/collect";
    }


    @RequestMapping("/checkCollect")
    @ResponseBody
    public Map checkCollect(@RequestBody Collect collect){
        System.out.println(collect);
        //查看某篇博客是否收藏
        return RespJson.pack(200,"...",bs.checkCollect(collect));
    }


    @RequestMapping("/addCollect/{bid}")
    @ResponseBody
    public Map addCollect(@PathVariable("bid") String bid){
        //查看某篇博客是否收藏
        return RespJson.pack(200,"...",bs.addCollect(bid));
    }

    @RequestMapping("/goType")
    public String goType(){
        return "client/type";
    }

    @RequestMapping("/addType")
    @ResponseBody
    public Map addType(@RequestBody Type type){
        Type add = ts.add(type);
        System.out.println(add);
        return RespJson.pack(200,"...",add!=null?ts.getTypeAll():null);
    }

    @RequestMapping("/upType")
    @ResponseBody
    public Map upType(@RequestBody Type type){
        boolean b = ts.upByIdType(type);
        System.out.println(b);
        return RespJson.pack(200,"...",b);
    }


    @RequestMapping("/findByPage/{start}")
    @ResponseBody
    public Map findByPage(@PathVariable("start") int start){
        return RespJson.pack(200,"...", ts.findByPage(start));
    }

    //取消收藏
    @RequestMapping("/cancelCollect/{id}")
    @ResponseBody
    public Map cancelCollect(@PathVariable("id") String id){
        return  RespJson.pack(200,"..",bs.cancelCollect(id));
    }


    @RequestMapping("/myBlog")
    public  String myBlog(){
        return "client/myboke";
    }


    @RequestMapping("/findByPageCollect/{start}")
    @ResponseBody
    public Map findByPageCollect(@PathVariable("start")int start){
        return RespJson.pack(200,"收藏信息",bs.findByPageCollect(start));
    }


    @RequestMapping("/findByContextId/{id}")
    @ResponseBody
    public Map findByContextId(@PathVariable("id")String id){
        Content content = bs.findByContextId(id);
        return RespJson.pack(200,"文章内容",content);
    }

    @RequestMapping("/update")
    @ResponseBody
    public Map update(@RequestBody Content content){
        boolean b = bs.upById(content);
        return RespJson.pack(200,"文章内容",b);
    }

    @RequestMapping("/upUser")
    @ResponseBody
    public Map upUser(@RequestBody User u){
        System.out.println(u);
        return RespJson.pack(200,"修改成功,重新登陆后生效",us.upUser(u));
    }

    @RequestMapping("/del")
    @ResponseBody
    public Map del(@RequestBody Blog blog){
        System.out.println(blog);
        return RespJson.pack(200,"删除信息",bs.del(blog));
    }


    @RequestMapping("/publish")
    public String publish(){
       return "client/addblog";
    }

    @RequestMapping("/addBlog")
    @ResponseBody
    public Map addBlog(@RequestBody BlogContent blog){
        blog.getBlog().setDigest("摘要：" + blog.getBlog().getDigest());
        Blog b = bs.addBlog(blog);
        return RespJson.pack(200,"用户信息",b);
    }

    @RequestMapping("/findByUidPage/{start}")
    @ResponseBody
    public Map findByUidPage(@PathVariable("start") int start){
        return RespJson.pack(200,"博客信息",bs.findByUidPage(start));
    }



}
